package cn.idietrade.idietradeesmodule9000.controller;

import cn.idietrade.common.utils.EmptyUtils;
import cn.idietrade.common.utils.ReturnResultData;
import cn.idietrade.common.utils.SecondPage;
import cn.idietrade.idietradeesmodule9000.service.goodsservice.GoodsService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @author zjy
 * @version 1.0
 * @createTime 2022-12-03 17:45
 * @description
 */
@RestController
public class ESController {
    @Resource
    private GoodsService goodsService;

    Logger logger= Logger.getLogger("ESController");

    @RequestMapping("/goodsAllAddES")
    public ReturnResultData goodsAllAddES(@RequestParam(value = "keyword",required = false)String keyword,
                                          @RequestParam(value = "cityId",required = false)Integer cityId){
        ReturnResultData resultData = new ReturnResultData<>();
        goodsService.goodsAllAddES(null,null);
        resultData.setCode(1);
        resultData.setMessage("数据同步成功");
        resultData.setData(null);
        logger.info("数据同步成功");
        return resultData;
    }

    @RequestMapping("/goodsShow")
    public ReturnResultData<SecondPage> goodsShow(@RequestParam(value = "pageNum",required = false)   Integer pageNum,
                                                                 @RequestParam(value = "pageSize",required = false)  Integer pageSize,
                                                                 @RequestParam(value = "keyword",required = false)   String keyword,
                                                                 @RequestParam(value = "cityId",required = false)    Integer cityId,
                                                                 @RequestParam(value = "goodsType",required = false) Integer goodsType){
        ReturnResultData<SecondPage> resultData = new ReturnResultData<>();
        SecondPage mapList=null;
        try {
            mapList = goodsService.goodsShow(pageNum, pageSize, keyword, cityId,goodsType);
            if(EmptyUtils.isEmpty(mapList)){
                resultData.setCode(0);
                resultData.setData(null);
                resultData.setMessage("没有数据");
                //ES数据为空时 调用商品模块的数据库查询 看是否有对应数据
                goodsService.goodsAllAddES("",keyword);
                //数据同步成功后再次向ES查询
                mapList=goodsService.goodsShow(pageNum,pageSize,keyword,cityId,goodsType);
                resultData.setCode(1);
                resultData.setData(mapList);
                resultData.setMessage("数据查询成功");
            }
            resultData.setData(mapList);
        }catch (Exception e){
            resultData.setCode(-1);
            resultData.setData(null);
            resultData.setMessage("程序异常");
            logger.info("--------程序异常-------"+e.getMessage());
            e.printStackTrace();
        }
        return resultData;
    }


}
